package com.xxxx.controller;

import com.xxxx.pojo.Customer;
import com.xxxx.pojo.Message;
import com.xxxx.pojo.ResponseResult;
import com.xxxx.service.PersonService;
import com.xxxx.utils.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * @Author Lionel
 * @Date 2021/3/9 22:28
 * Created by IntelliJ IDEA 2020.1.
 * Project: house
 * Description：用户Controller
 */
@Controller
@RequestMapping("/person")
public class PersonController extends BaseController{
    @Autowired
    private PersonService personService;

    /**
     * 跳转用户页面
     * @return 用户页面
     */
    @GetMapping("/toPerson")
    public String toPerson() {
        return "person";
    }

    /**
     * 跳转用户信息编辑页面
     * @return 信息页面
     */
    @GetMapping("/toEditPerson")
    public String toEditPerson() {
        return "person/edit-person";
    }

    /**
     * 跳转用户认证页面
     * @return 认证页面
     */
    @GetMapping("/toCertifyPerson")
    public String toCertifyPerson() {
        return "person/certify-person";
    }

    /**
     * 跳转用户密码编辑页面
     * @return 密码编辑页面
     */
    @GetMapping("/toChangePassword")
    public String toChangePassword() {
        return "person/change-password";
    }

    /**
     * 跳转用户消息管理页面
     * @return 消息管理页面
     */
    @GetMapping("/toMessagePerson")
    public String toMessagePerson() {
        return "person/message-person";
    }

    @PostMapping("/selectCustomer")
    @ResponseBody
    public ResponseResult<Customer> selectCustomer (
            HttpSession session
    ) {
        ResponseResult<Customer> result = new ResponseResult<>();
        Customer customer = personService.selectCustomer(session);
        result.setData(customer);
        return result;
    }

    @PostMapping("/updateCustomer")
    @ResponseBody
    public ResponseResult<Void> updateCustomer (
            HttpSession session,
            HttpServletRequest request,
            MultipartFile avatarFile,
            @RequestParam("nickname") String nickName,
            @RequestParam("phone") String phone,
            @RequestParam("email") String email
    ) throws IOException {
        Customer customer = new Customer();
        customer.setCmNickname(nickName);
        customer.setCmPhone(phone);
        customer.setCmEmail(email);
        System.out.println(customer);

        // 上传头像
        if (avatarFile.getSize() != 0) {
            String filePath = UploadFile.uploadFile(request, avatarFile);
            customer.setCmHeadPhoto(filePath);
        } else {
            String originImg = personService.selectCustomer(session).getCmHeadPhoto();
            customer.setCmHeadPhoto(originImg);
        }
        personService.updateCustomer(session, customer);
        return new ResponseResult<>();
    }

    @PostMapping("/updatePassword")
    @ResponseBody
    public ResponseResult<Void> updatePassword (
            HttpSession session,
            @RequestParam("oldPassword") String oldPassword,
            @RequestParam("newPassword") String newPassword
    ) {
        personService.updatePassword(session, oldPassword, newPassword);
        return new ResponseResult<Void>();
    }


    @PostMapping("/selectCertifyCustomer")
    @ResponseBody
    public ResponseResult<Customer> selectCertifyCustomer (
            HttpSession session
    ) {
        ResponseResult<Customer> result = new ResponseResult<>();
        Customer customer = personService.selectCertifyCustomer(session);
        result.setData(customer);
        return result;
    }
    @PostMapping("/certifyCustomer")
    @ResponseBody
    public ResponseResult<Void> certifyCustomer (
            HttpServletRequest request,
            HttpSession session,
            MultipartFile cardPhoto,
            @RequestParam("name") String name,
            @RequestParam("cardID") String cardID
    ) throws IOException {
        Customer customer = new Customer();
        customer.setCmName(name);
        customer.setCmCardID(cardID);
        customer.setCmStatus(2);
        String filePath = new String();
        if (cardPhoto.getSize() != 0) {
            filePath = UploadFile.uploadFile(request, cardPhoto);
            System.out.println(filePath);
        } else {
            filePath = "/upload/list-head.png";
        }
        customer.setCmCardPhoto(filePath);
        personService.certifyCustomer(session, customer);
        return new ResponseResult<Void>();
    }

    @PostMapping("/getCertifyStatus")
    @ResponseBody
    public ResponseResult<Integer> getCertifyStatus (HttpSession session) {
        ResponseResult<Integer> result = new ResponseResult<>();
        Integer data = personService.getCertifyStatus(session);
        result.setData(data);
        return result;
    }


    @PostMapping("/getAllMessage")
    @ResponseBody
    public ResponseResult<List<Message>> getAllMessage () {
        ResponseResult<List<Message>> result = new ResponseResult<>();
        List<Message> list = personService.getAllMessage();
        result.setData(list);
        return result;
    }

    @PostMapping("/getSingleMessage")
    @ResponseBody
    public ResponseResult<String> getSingleMessage (
            @RequestParam("id") Integer id
    ) {
        ResponseResult<String> result = new ResponseResult<>();
        String message = personService.getSingleMessage(id);
        result.setData(message);
        return result;
    }

    @PostMapping("/deleteSingleMessage")
    @ResponseBody
    public ResponseResult<Void> deleteSingleMessage (
            @RequestParam("id") String id
    ) {
        Integer mid = Integer.parseInt(id);
        personService.deleteSingleMessage(mid);
        return new ResponseResult<>();
    }


}
